package sk.stuba.fiit.ltasys.web.mvc.controller;


import java.io.StringWriter;

import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import sk.stuba.fiit.ltasys.web.mvc.model.entity.ArchivePackageEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.service.TestService;
import sk.stuba.fiit.ltasys.web.mvc.model.service.core.ArchiveCreator;
import sk.stuba.fiit.ltasys.web.mvc.model.service.core.ArchiveSignatureVerifier;
import sk.stuba.fiit.ltasys.web.mvc.model.service.core.PackageForUserCreator;
import sk.stuba.fiit.ltasys.ws.schema.beans.ArchiveResponse;


/**
 * TestController controller.
 * 
 * @author Marek Hruska
 */
@Controller
public class TestController {
	
	private static final String TESTPAGEVIEW = "test/testPage";
	
	@Autowired 
	private TestService testService;
	
	
	@Autowired
	private ArchiveCreator archiveCreator;
	
	@Autowired 
	private ArchiveSignatureVerifier archiveSignature;
	
	@Autowired
	private PackageForUserCreator packageCreator;
	
	@Autowired 
	private Jaxb2Marshaller marshallerService;
	
    @RequestMapping(value="/test/testPage", method=RequestMethod.GET)
    public String getTestPage() {
  
    	testPKIComunication();
    	
    	return TESTPAGEVIEW;
    }


	private void testPKIComunication() {
		try {
		
		ArchivePackageEntity archivPackage = testService.XtestLoadArchivePackage(null);
		ArchiveResponse response = packageCreator.assembleArchivePackage(archivPackage);
		archiveSignature.verifieArchiveSignature(archivPackage);
		
			archiveCreator.prolongProtection(archivPackage);
			testService.XtestSaveArchivePackage(archivPackage);
			archiveSignature.verifieArchiveSignature(archivPackage);
	
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("comunication OK");
	}
    
}
    
	
	
    

